package controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import bean.Aktie;
import bean.User;
import dao.AktieDAO;
import view.MyPortfolioDataset;

public class MyPortfolioController {
	
	public MyPortfolioController() {
		
	}
	
	
	public ArrayList<MyPortfolioDataset> getPortfolioList(User user){
		try {
			ArrayList<MyPortfolioDataset> portfolioList = new ArrayList<>();

			ArrayList<Aktie> allAktienFromUser;
			ArrayList<String> allAnbieter;

			allAktienFromUser = AktieDAO.getInstance().getAktienFromUser(user);
			allAnbieter = AktieDAO.getInstance().getAllAnbieter();

			int anzahl;
			MyPortfolioDataset tempMyPortfolioDataset;
			if (allAktienFromUser != null) {

				for (String currAnbieter : allAnbieter) {
					anzahl = 0;
					tempMyPortfolioDataset = null;

					for (Aktie currAktie : allAktienFromUser) {

						if (currAktie.getAnbieterName().equals(currAnbieter)) {
							anzahl++;
							tempMyPortfolioDataset = new MyPortfolioDataset(
									currAktie.getAnbieterKuerzel(),
									currAktie.getAnbieterName(),
									String.valueOf(anzahl),
									String.valueOf(currAktie.getDividende()));
						}

					}

					if (anzahl > 0) {
						portfolioList.add(tempMyPortfolioDataset);
					}

				}
				return portfolioList;
			} 
			else {
				return null;
			}

		} catch (SQLException | IOException e) {
			e.printStackTrace();
			return null;
		}

	}
	
	public String getKontostandByUserPK(User user){
		
		try {
			return String.valueOf(AktieDAO.getInstance().getKontostandByUserPK(user.getUserPK()));
		} catch (SQLException | IOException e) {
			return null;
		}
		
	}
	
}
